package com.topisv.tms.utils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class SystemUtil {

	public static String filePath_WEB_INF = "WEB-INF";
	public static String fileName_views = "views";
	public static String file_tracking = "tracking";// 快递模板存放目录

	public static String file_uploadImage = "images";// 文件存放目录
	public static String file_exepress_style = "expressStyle";// 快递单目录
	public static String file_packList = "packList";// 随箱单目录

	public static String file_static = "static";
	
	public static String keyvalue="PYDZSW";
	
	public static String partner="20130617154725409";
	
	public static String sign_type="MD5";
	
	public static String service_code="SZPY-002";
	
	public static String wlb_url="http://wlb.taobao.com:80/cooperate/gateway.do";
	
	private static final Logger logger = LoggerFactory.getLogger(SystemUtil.class);

	/**
	 * 获取系统文件分隔符
	 * 
	 * @return
	 */
	public static String getFileSeparator() {
		return System.getProperty("file.separator");
	}
	
	
	public static String pub(String serviceURL, String param) {
		URL url = null;
		HttpURLConnection connection = null;
		StringBuffer buffer = new StringBuffer();
		try {
			url = new URL(serviceURL);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Length",param.length()+"");
			connection.setRequestProperty("Content-Type", "text/json;charset=utf-8");
			// connection.setRequestProperty("User-Agent","Java/1.6.0_29");
			//connection.connect();
			OutputStream os=connection.getOutputStream();
	        DataOutputStream dos=new DataOutputStream(os);
	        dos.write(param.getBytes("UTF-8"));
	        dos.flush();
	        dos.close(); 
			/*OutputStream outputStream = connection.getOutputStream();
			outputStream.write(param.getBytes("UTF-8"));
			outputStream.flush();
			outputStream.close();*/
			InputStream inputStream = connection.getInputStream();
			byte[] bytes = new byte[1024];
			int count = inputStream.read(bytes);
			while (count != -1) {
				buffer.append(new String(bytes, 0, count, "UTF-8"));
				count = inputStream.read(bytes);
			}
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
			
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return buffer.toString();
	}

	/**
	 * 获得系统换行
	 * 
	 * @return
	 */
	public static String getLineSeparator() {
		return System.getProperty("line.separator");
	}

	public static String getUUid() {
		return UUID.randomUUID().toString();
	}
	
	/** 
     *  
     * 创建日期2011-4-25上午10:12:38 
     * 修改日期 
     * 作者：kk *TODO 使用Base64加密算法加密字符串 
     *return 
	 * @throws  
     */  
    public static String getBASE64(String str){  
    	try {
	        byte[] b = str.getBytes("UTF-8");
	        Base64 base64=new Base64();  
	        b=base64.encode(b);  
	        String s=new String(b);  
	        return s;  
    	} catch (Exception e) {
			logger.error("error",e);
		}  
    	return null;
    }  
//	
//	public static String getBASE64(String str) {
//	   String s = null;
//	   if (str != null) {
//	    try {
//			s = new sun.misc.BASE64Encoder().encode(str.getBytes("UTF-8"));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			
//		}
//	  }
//	  return s;
//	}
	
	public static String MD5(String s) {

        try {
        	MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(s.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
               i = b[offset];
               if (i < 0)
                  i += 256;
               if (i < 16)
                  buf.append("0");
               buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (Exception e) {
            
            return null;
        }
    }
	
	/**
	 * 获取机器名称
	 * @return String
	 */
	public static String getMachineName(){
		String name = ManagementFactory.getRuntimeMXBean().getName();
		return name.substring(name.indexOf("@") + 1);
	}
	
	public static void main(String[] args) {
    }

}
